package com.zxw.flow.aspect;

import com.zxw.common.core.base.constant.JavaBaseTypeConstants;
import com.zxw.common.web.util.InitPropertyValueUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.Configuration;

/**
 * @author Xuewei.Zhang
 * @date 2022/3/14
 */

@Aspect
@Configuration
@Slf4j
public class InitUpdateAspect {

    @Pointcut(value = "@annotation(com.zxw.common.web.annotation.InitUpdateInfo)")
    public void pointcutService() {

    }

    @Before("@annotation(com.zxw.common.web.annotation.InitUpdateInfo)")
    public void doBefore(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        if (args.length == 0)
            return;

        if (args.length == 1) {
            InitPropertyValueUtils.initUpdateValue(args[0]);
            return;
        }

        for (int i = 0; i < args.length; i++) {
            Object arg = args[i];
            if (!JavaBaseTypeConstants.types.contains(arg.getClass().getTypeName())) {
                InitPropertyValueUtils.initUpdateValue(args[i]);
            }
        }
    }

}
